package com.cn.mmd.controller;

import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 管理教师组
 * Created by Administrator on 2016/8/4 0004.
 */
@Controller
@RequestMapping(value = "page/manageTeachingGroup")
public class ManageTeachingGroupController {

    @Autowired
    private TeachingGrupServiceL teachingGrupServiceL;

    @Autowired
    private LogManageService logManageService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    EvaluateTeacherService evaluateTeacherService;

    @Autowired
    private TGroupLService tGroupLService;



    /**
     * 为教学组添加一个老师并设置职务
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/addTea")
    @ResponseBody
    public synchronized Object manageTeachingGroupAdd(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        Integer teaGroupId = Integer.parseInt(request.getParameter("teaGroupId"));
        String teacherId = request.getParameter("teacherId");
        String teachingGroupPosition = request.getParameter("teachingGroupPosition");
        TeachingGroup teachingGroup = new TeachingGroup();
        teachingGroup.setTeacherid(teacherId);
        teachingGroup.setTeachinggroupid(teaGroupId);
        List<TeachingGroup> teachingGroupList1 = teachingGrupServiceL.selectByTeacherGroupId1(teachingGroup);
        if (teachingGroupList1!=null&&teachingGroupList1.size()==0) {
            teachingGroup.setTeachinggroupposition(teachingGroupPosition);
            if (teachingGroupPosition != null && teachingGroupPosition.equals("管理")) {
                teachingGroup.setTeachinggroupmanageid(teacherId);
                List<TeachingGroup> teachingGroupList = teachingGrupServiceL.selectByTeachingGroupId(teaGroupId);
                if (teachingGroupList != null && teachingGroupList.size() > 0 && teachingGroupList.get(0).getTeachinggroupmanageid() != null) {
                    objectMap.put("code", 201);
                    objectMap.put("message", "此教学组已经有管理了");
                } else {
                    if (teachingGrupServiceL.insertSelective(teachingGroup) > 0) {
                        TeachingGroup teachingGroup1 = new TeachingGroup();
                        teachingGroup1.setTeachinggroupid(teaGroupId);
                        teachingGroup1.setTeachinggroupmanageid(teacherId);
                        teachingGrupServiceL.updateByTeachingGroupId(teachingGroup1);
                        objectMap.put("code", 200);
                        objectMap.put("message", "成功");
                        Date date = new Date();
                        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        String logTime = time.format(date);
                        LogManage logManage = new LogManage();
                        logManage.setLogtime(logTime);
                        logManage.setManageid(manageId);
                        logManage.setLogcontent("给教学组分配了一个老师:" + teaGroupId);
                        logManageService.insertSelective(logManage);
                    } else {
                        objectMap.put("code", 201);
                        objectMap.put("message", "失败");
                    }
                }
            } else {
                List<TeachingGroup> teachingGroupList = teachingGrupServiceL.selectByTeachingGroupId(teaGroupId);
                if (teachingGroupList != null &&teachingGroupList.size()>0 && teachingGroupList.get(0).getTeachinggroupmanageid() != null) {
                    teachingGroup.setTeachinggroupmanageid(teachingGroupList.get(0).getTeachinggroupmanageid());
                }
                if (teachingGrupServiceL.insertSelective(teachingGroup) > 0) {
                    objectMap.put("code", 200);
                    objectMap.put("message", "成功");
                    Date date = new Date();
                    SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String logTime = time.format(date);
                    LogManage logManage = new LogManage();
                    logManage.setLogtime(logTime);
                    logManage.setManageid(manageId);
                    logManage.setLogcontent("给教学组分配了一个老师:" + teaGroupId);
                    logManageService.insertSelective(logManage);
                } else {
                    objectMap.put("code", 201);
                    objectMap.put("message", "失败");
                }
            }
        }else {
            objectMap.put("code", 201);
            objectMap.put("message", "失败请勿重复将一个老师分配至这个组");
        }
        return objectMap;
    }

    /**
     * 修改老师的职务和教师分组
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/upd")
    @ResponseBody
    public synchronized Object manageTeachingGroupUpd(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        Integer groupId = Integer.parseInt(request.getParameter("GroupId"));
        Integer teaGroupId = Integer.parseInt(request.getParameter("teaGroupId"));
        String teachingGroupPosition = request.getParameter("teachingGroupPosition");
        TeachingGroup teachingGroup = new TeachingGroup();
        teachingGroup.setGroupid(groupId);
        teachingGroup.setTeachinggroupid(teaGroupId);
        teachingGroup.setTeachinggroupposition(teachingGroupPosition);
        if (teachingGrupServiceL.updateByPrimaryKeySelective(teachingGroup) > 0) {
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            Date date = new Date();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime = time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("修改了老师在教学组内的信息:" + teaGroupId);
            logManageService.insertSelective(logManage);
        } else {
            objectMap.put("code", 201);
            objectMap.put("message", "失败");
        }
        return objectMap;
    }

    /**
     * 删除老师教学组信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public synchronized Object manageTeachingGroupDel(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String teacherId = request.getParameter("teacherId");
        if (teachingGrupServiceL.deleteByTeaId(teacherId) > 0) {
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            Date date = new Date();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime = time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("删除了"+ teacherId + "的教学组信息:" );
            logManageService.insertSelective(logManage);
        } else {
            objectMap.put("code", 201);
            objectMap.put("message", "成功");
        }
        return objectMap;
    }

    @RequestMapping(value = "/sel")
    @ResponseBody
    public synchronized Object manageTeachingGroupSel(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String,Object> map;
        String teacherId = request.getParameter("teacherId");
        Integer startrow = Integer.parseInt(request.getParameter("startrow"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String manageId = request.getParameter("manageId");
        Teacher teacher = new Teacher();
        teacher.setTeacherid(teacherId);
        teacher.setStartrow(startrow);
        teacher.setPageSize(pageSize);
        teacher.setTeacherposition("离职");
        List<Teacher> teacherList = teacherService.selectByPaging(teacher);
        if (teacherList!=null&&teacherList.size()>0){
                List<TeachingGroup> teachingGroup;
            TGroup tGroup;
            for (int i = 0 ; i < teacherList.size() ; i ++) {
                map = new HashMap<String, Object>();
                teacher = teacherList.get(i);
                map.put("teacherId", teacher.getTeacherid());
                map.put("teacherIdCard", teacher.getTeacheridcard());
                map.put("teacherName", teacher.getTeachername());
                map.put("teacherCollege", teacher.getTeachercollege());
                map.put("teacherPhoneNumber", teacher.getTeacherphonenumber());
                map.put("teacherSchool", teacher.getTeacherschool());
                map.put("teacherSex", teacher.getTeachersex());
                map.put("teacherPosition", teacher.getTeacherposition());
                teachingGroup = evaluateTeacherService.selectTeachingGroupId(teacher.getTeacherid());
                if (teachingGroup!=null&&teachingGroup.size()>0){
                    String s = "";
                    for (int j = 0 ; j < teachingGroup.size();j++){
                        tGroup = tGroupLService.selectByPrimaryKey(teachingGroup.get(j).getTeachinggroupid());
                        s = tGroup.getTeachinggroupname()+","+s;
                    }
                    map.put("teacherGroup",s);
                }else {
                    map.put("teacherGroup","暂无分组");
                }
                mapList.add(map);
            }
            if (mapList!=null&&mapList.size()==teacherList.size()){
                objectMap.put("code",200);
                objectMap.put("message","成功");
                objectMap.put("data",mapList);
                teacher = new Teacher();
                teacher.setTeacherposition("离职");
                teacher.setTeacherid(teacherId);
                objectMap.put("total",teacherService.countLimitNum(teacher));
            }else {
                objectMap.put("code",201);
                objectMap.put("message","失败");
            }
        }else {
            objectMap.put("code",202);
            objectMap.put("message","教师列表为空");
        }

        return objectMap;
    }

}
